import {legacy_createStore,applyMiddleware} from "redux"
import thunk from "redux-thunk"
import logger from "redux-logger"
import {ALLTYPES,ALLCKTYPES,CS,ADD,LISTTYPES,DEL} from "../types/store.d"
const initialState = {
    list:[],
    shop:[]
}

const reduce= (state: ALLTYPES = initialState, { type, payload }:ALLCKTYPES) => {
    switch (type) {

    case CS:
        state.list.push(payload)
        return { ...state }
    
    case ADD:
        const newlist=[...state.list]
        newlist.forEach((item:any,index)=>{
        if(item.id===payload.id){
            item.num++
        }
    })
    state.list=newlist
    return { ...state }
    case DEL:
        const newarr=[...state.list]
        newarr.forEach((item:any,index)=>{
        if(item.id===payload.id){
            if(item.num>1){
                item.num--
            }
        }
    })
    state.list=newarr
    return { ...state }
    default:
        return state
    }
}
const store=legacy_createStore(reduce,applyMiddleware(thunk,logger))

export default store